package com.example.pan.ui.file

import android.content.Context
import android.text.TextUtils
import android.util.AttributeSet
import android.view.Gravity
import android.view.SurfaceHolder
import android.view.SurfaceView
import android.widget.FrameLayout
import tv.danmaku.ijk.media.player.IMediaPlayer
import tv.danmaku.ijk.media.player.IjkMediaPlayer

class MediaPlayer @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
    FrameLayout(context, attrs) {

    private var mPlayer: IjkMediaPlayer? = null
    private var mPlayerListener: MediaPlayerListener? = null
    private var mSurfaceView: SurfaceView? = null
    private var mPath: String? = ""
    private val mHeaders: HashMap<String, String> = HashMap()

    init {
        isFocusable = true

    }

    private fun initSurfaceView() {
        this.mSurfaceView = SurfaceView(context).also {
            it.holder.addCallback(object : SurfaceHolder.Callback {
                override fun surfaceCreated(holder: SurfaceHolder) {
                }

                override fun surfaceChanged(
                    holder: SurfaceHolder,
                    format: Int,
                    width: Int,
                    height: Int
                ) {
                    loadMedia()
                }

                override fun surfaceDestroyed(holder: SurfaceHolder) {
                }

            })
            val layoutParams =
                LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, Gravity.CENTER)
            it.layoutParams = layoutParams
            this.addView(it)
        }

    }

    private fun loadMedia() {
        this.mPlayer?.stop()
        this.mPlayer?.release()
        this.mPlayer = IjkMediaPlayer().also {
            this.mPlayerListener?.let { listener ->
                it.setOnPreparedListener(listener)
                it.setOnErrorListener(listener)
            }
            it.setOnVideoSizeChangedListener { _, width, height, _, _ ->
                this.mSurfaceView?.layoutParams =
                    this.mSurfaceView?.layoutParams.also { layoutParams ->
                        layoutParams?.width = width
                        layoutParams?.height = height
                    }
            }
            it.setDataSource(this.mPath, this.mHeaders)
            it.setDisplay(this.mSurfaceView?.holder)
            it.prepareAsync()
        }
    }


    fun setPath(path: String, headers: Map<String, String>) {
        mHeaders.putAll(headers)
        if (TextUtils.equals("", this.mPath)) {
            mPath = path
            initSurfaceView()
        } else {
            mPath = path
            loadMedia()
        }
    }

    fun setPlayerListener(playerListener: MediaPlayerListener) {
        this.mPlayerListener = playerListener;
        this.mPlayerListener?.let {
            mPlayer?.setOnPreparedListener(it)
        }
    }

    fun destory() {
        this.mPlayer?.stop()
        this.mPlayer?.release()
    }


    interface MediaPlayerListener : IMediaPlayer.OnPreparedListener,
        IMediaPlayer.OnCompletionListener,
        IMediaPlayer.OnErrorListener

}